<?php
/*
习题管理模块
*/
include_once('include/top.php');
$action=fs_get_value("do");
switch($action)
{
	case	"addtest"	     : addtest()	         ; break;
	case	"add"	         : do_addtest()	         ; break;
	case	"delete"	     : delete_test()	     ; break;
	case 	"editquestion"   : editquestion()        ; break;
	case    "edit_question"  : edit_question()       ; break;
	case    "do_edit_question": do_edit_question()   ; break;
        case    "batimport"    :import();break;
        case    "doimport" :doimport();break;
	case    "Mancat"         : veiwcat()             ; break;
        case    "getparts"        :getparts()            ;break;
        case    "delete_question"        :do_delete_question()            ;break;
	default				     : main()		         ; break;
}
?>

<script type="text/javascript">
function Check(){
    if(document.addform.test_name.value ==  ""){
        alert("习题标题不能为空!");
        document.addform.test_name.focus();
        return  false;
    }
    if(document.addform.test_type.value ==  ""){
        alert("习题等级不能为空!");
        document.addform.test_type.focus();
        return  false;
    }
}
function addcheck()
{
    if(FCKeditorAPI.GetInstance('title').EditorDocument.body.innerText ==  ""){
        alert("题目标题不能为空!");
        return  false;
    }
    if(document.add_questionform.answer.value ==  ""){
        alert("请填写正确答案!");
        document.add_questionform.answer.focus();
        return  false;
    }
    if(FCKeditorAPI.GetInstance('test_answer').EditorDocument.body.innerText ==  ""){
        alert("题目答案不能为空!");
        return  false;
    }
}
</script>
<script>
function isdel(o){
	var i = 0;
	var j = 0;
	for(i=0;i< document.getElementsByName(o).length;i++)
	{
		if(document.getElementsByName(o)[i].checked == true )
		{
			j++;
		}
	}
	if(j == 0)
	{
		alert("请选中要删除的习题！");
		return false;
	}
	else
	{
	    if (confirm('删除习题会将习题中的题目和答案一齐删除。你确定删除吗？'))
	    {
		    return true;
	    }
	    else
	    {
		    return false;
	    }
	}
}
</script>
<?php

//***********************************************************
//**********************管理习题*****************************
//***********************************************************
function main()
{
        /*global $db;
	$keywords = fs_get_value("KeyWords");
	$test_major = fs_get_value("test_major");
	//$test_major = fs_get_value("test_major");
	if($keywords != null && $test_type != null){
		$sql = "SELECT * FROM fooeeexam_exam where title like '%".$keywords."%' and exam_major = ".$test_major."";
	}
	elseif($keywords != null && $test_type == null){
		$sql = "SELECT * FROM fooeeexam_exam where exam_name like '%".$keywords."%'";
	}
	elseif($keywords == null && $test_type != null){
		$sql = "SELECT * FROM fooeeexam_exam where exam_major = ".$test_major."";
	}
	else{
		$sql = "select * from fooeeexam_exam order by exam_id desc";
	}
	$test_result = $db -> query($sql);
	$recordcount = $db -> num_rows($test_result);
	@$page=$_GET['p'];
	if ($page==null){$page=1;}
	$pagesize=12;								//每页显示数
	$pagecount=ceil($recordcount/$pagesize);	//计算总页
	if ($page>$pagecount){$page=$pagecount;}
	if ($page<1){$page=1;}*/
?>
<script src="./js/Main.js"></script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  <tr>
    <th class="CTitle">题库管理</th>
  </tr>
  <tr>
    <td class="CPanel">
    <div class="SubMenu">
		<a href="admin_test.php?">题库管理</a> | 
		<a href="admin_test.php?do=addtest">添加习题</a> | 
                <a href="admin_test.php?do=batimport">批量导入</a>|
                <a href="admin_test.php?do=batimport">生成试卷</a>
	</div>
	
	<form action="?do=delete" method="post" name="fooeeform" style="margin:0px">
		<table border="0" cellpadding="0" cellspacing="0"  class="TablePanel" width="100%">
			<tr align="center" class="TDHead">
				<td nowrap><input type=button style="font-size:9pt;width:50px;" value="全选" onclick="click_allid(fooeeform,1)"/></td>
				<td nowrap><input type=button style="font-size:9pt;width:60px;" value="全取消" onclick="click_allid(fooeeform,0)"/></td>
				<td nowrap><input type="submit" style="font-size:9pt;width:50px;" value="删除" onclick="return isdel('id[]')"/></td>
				<td nowrap><input type=text name="KeyWords" value="<?php echo $keywords;?>" style="width:160px;"/>
					<input type=button style="font-size:9pt;width:80px;" onclick="window.location.href('?KeyWords='+document.fooeeform.KeyWords.value);" value="搜索习题" />
				</td>
				<td nowrap>.</td>
				<td nowrap>.</td>
				<td width="30%" nowrap></td>
				<td nowrap></td>
			</tr>
		</table>
<div id="click_idall">
    <div align="left" style="padding:5px;">
      <table border="0" cellpadding="0" cellspacing="0" class="TablePanel" align="center" width="100%">
        <tr align="center">
		  <td class="TDHead">√</td>
          <td class="TDHead" nowrap>习题标题</td>
         <!-- <td class="TDHead" nowrap>习题专业</td>-->
          <td class="TDHead" nowrap>试题来源</td>
          <td class="TDHead" nowrap>现有题目数量</td>
          <td class="TDHead" nowrap>参考人数</td>
		  <td class="TDHead" nowrap>操作</td>
          <td class="TDHead" nowrap>管理题目分类</td>
        </tr>
<?php
	/*if($keywords != null && $test_type != null){
		$sql = "SELECT * FROM fooeeexam_exam where exam_name like '%".$keywords."%' and exam_major = ".$test_major." order by datetime desc limit ".($page-1)*($pagesize).",$pagesize";
	}
	elseif($keywords != null && $test_type == null){
		$sql = "SELECT * FROM fooeeexam_exam where exam_name like '%".$keywords."%'  limit ".($page-1)*($pagesize).",$pagesize";
	}
	elseif($keywords == null  && $test_type != null){
		$sql = "SELECT * FROM fooeeexam_exam where exam_major = ".$test_major." limit ".($page-1)*($pagesize).",$pagesize";
	}
	else{
		$sql = "select * from fooeeexam_exam order by exam_id desc limit ".($page-1)*($pagesize).",$pagesize";
	}
	$test_result=$db->query($sql);
	while($test_row = $db->fetch_array($test_result))
	{
?>
		<tr id="t<?php echo($test_row["exam_id"]);?>" onclick="click_tr('<?php echo ($test_row["exam_id"]);?>')" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='FFFFFF'" title=单击选中 align="center">
			<td><input name='id[]' type='checkbox' id="c<?php echo($test_row["exam_id"]);?>" value='<?php echo ($test_row["exam_id"]);?>' onclick="click_tr('<?php echo ($test_row["exam_id"]);?>')"></td>
			<td><a href="?do=addtest&ID=<?php echo ($test_row["exam_id"]);?>"><?php echo($test_row["exam_name"]);?></a></td>
			<td><?php if($test_row['exam_source'] == '') echo("来自实训网"); echo($test_row['exam_source']);?></td>
			<td><?php 
                        echo(get_question_num('exam_id',$test_row["exam_id"]));
                        ?></td>
			<td><?php echo($test_row["exam_total_finished"]);?></td>
			<td><a href="?do=addtest&ID=<?php echo ($test_row["exam_id"]);?>">编辑</a></td>
			<td><a href="?do=Mancat&ID=<?php echo ($test_row["exam_id"]);?>">管理</a></td>
		</tr>
		<tr>
			<td height=1 colspan=9 bgcolor="#E8F1FF" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='E7sxE7sxE7sx'"></td>
		</tr>
<?php
	}*/
?>
	</table>
	</form>
	
	
	
	</div>
	<center>
<?php
/*分页示例:

pager(传入URL , 数据总数 , 开始条数 , 每页数目)
*/
echo(pager('?p={page}&KeyWords='.$keywords.'&test_major='.$test_major.'&test_type='.$test_type,$recordcount,($page-1)*($pagesize),$pagesize));		
?>
</center>
	</td>
	</tr>
	</table>

<?php
}
function get_test_major($test_major){
    global $db;
	$sql = "select * from 7sx_major";
	$result = $db -> query($sql);
	while($row = $db -> fetch_array($result)){
		if($test_major == $row['major_id']){
			$str.= '<option value ="'.$row['major_id'].'" selected>'.$row['name'].'</option>'."\n";
		}
		else
		{	
		     $str.= '<option value ="'.$row['major_id'].'">'.$row['name'].'</option>'."\n";
		}
	}
	return $str;
}
function get_test_major_name($major_id){
    global $db;
	$sql = "select * from 7sx_major where major_id = ".$major_id;
	$result = $db -> query($sql);
	$row = $db -> fetch_array($result);
	return $row['name'];
}
?>


<!--
编缉习题标题开始
-->
<?php
function addtest()
{
        global $db;
	$test_id = intval(fs_get_value("ID"));
        $sql="select * from fooeeexam_classification";
        $cates =$db -> query($sql);
	if($test_id != ''){
		$sql = "select * from fooeeexam_exam where exam_id = ".$test_id;
		$test_result = $db -> query($sql);
		$test_row = $db -> fetch_array($test_result);
	}
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">习题管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
			<div class="SubMenu">
				<a href="admin_test.php?">习题管理</a> | 
				<a href="admin_test.php?do=addtest">添加习题</a> | 
				<a href="admin_test_record.php?">习题记录</a>|
                                <a href="admin_test.php?do=batimport">批量导入</a>
			</div>
			</td>
		</tr>	
		<tr>
			<td>
                            <!--试题添加-->
                            <script type="text/javascript">
                                function selecttype(value){
                                    if(value==1||value==2) {
                                        $("#examtype2").css("display","block");} 
                                    else{
                                        $("#examtype2").css("display","none");
                                        $("#cate").css("display","none");
                                    }
                                }
                                function selecttype2(value){
                                    if(value==2){
                                        $("#cate").css("display","block");
                                    }
                                    else{
                                        $("#cate").css("display","none");
                                    }
                                }
                            </script>
                            <div class="test-box">
                                <ul>
                                    <form name="addform" method="post" action="?do=add&test_id=<?php echo($test_id);?>">
                                    <li>习题标题：<input name="test_name" type="text" size="30" class="text" value="<?php if($test_row['exam_name'] !== '') echo($test_row['exam_name']);?>"></li>
                                    <li>试题来源：<input name="test_per_name" type="text" size="30" class="text" value="<?php if($test_row['exam_source'] !== '')  echo($test_row['exam_source']);?>"></li>
                                    <li>考试时间：<input name="test_time" type="text" size="30" class="text" value="<?php if($test_row['exam_time'] !== '')  echo($test_row['exam_time']);?>"><span style="color:red">##</span>考试所需要的时间,只需要填数字,如120,以分钟为计算单位<span style="color:red">##</span></li>
                                    <li>需要点数：<input name="test_point" type="text" size="30" class="text" value="<?php if($test_row['exam_needmoney'] !== '')  echo($test_row['exam_needmoney']);?>"><span style="color:red">##</span>请填写纯数字<span style="color:red">##</span></li>
                                    <li>试卷年份：<input name="test_year" type="text" size="30" class="text" value="<?php if($test_row['exam_year'] !== '')  echo($test_row['exam_year']);?>"><span style="color:red">##</span>不要加单位年,正确写法如：2012<span style="color:red">##</span></li>
                                    <li>及格分数：<input name="test_score" type="text" size="30" class="text" value="<?php if($test_row['exam_basepoint'] !== '')  echo($test_row['exam_basepoint']);?>"><span style="color:red">##</span>填写纯数字，如：60<span style="color:red">##</span></li>
                                    <li>试卷分数：<input name="test_total_score" type="text" size="30" class="text" value="<?php if($test_row['exam_total_point'] !== '')  echo($test_row['exam_total_point']);?>"><span style="color:red">##</span>填写纯数字，如：100<span style="color:red">##</span></li>
                                    <li style="height:30px;">
                                        <div id="examtype" style="float:left;">
                                        试题类型1:<select name="exam_1" id="exam_1" onchange="selecttype(this.value)">
                                            <option value="0" >选择试题类型</option>
                                            <option value="1">模拟试题</option>
                                            <option value="2">历年真题</option>
                                        </select>
                                        </div>
                                        <div id="examtype2" style="float:left;display: none;">
                                        试题类型2:<select name="exam_2" id="exam_2" onchange="selecttype2(this.value)">
                                            <option value="0" >选择试题类型</option>
                                            <option value="1">全国试题</option>
                                            <option value="2">地方试题</option>
                                        </select>
                                        </div>
                                       
                                        <div id="cate" style="float:left;display: none;">
                                        所属省份：<select name="cate">
                                            <option value="0" >选择所属省份</option>
                                             <?php
                                            while($cate=$db->fetch_array($cates)){?>
                                            <option value="<?php echo $cate['class_id']?>"><?php echo $cate['class_name']?></option>
                                            <?php } ?>
                                        </select></div>
                                    </li>
                                    <li><input type="submit" value="提交" />&nbsp;&nbsp;<input type="reset" value="重置" /></li>
                                    </form>
                                </ul>
                            </div>                       
			
			</td>
		</tr>
	</table>
<?php
}
function do_addtest(){
    global $db;
	$test_name = fs_get_value("test_name");
	$exam_source = fs_get_value("test_per_name");
	$test_time = intval(fs_get_value("test_time"));
        $test_point = fs_get_value("test_point");
        //echo $test_point;exit();
        $test_year = intval(fs_get_value("test_year"));
        $test_score = intval(fs_get_value("test_score"));
        $test_total_score = intval(fs_get_value("test_total_score"));
        $cate = fs_get_value("cate");
        $exam_type_1= fs_get_value("exam_1");//1：模拟  2：真题
        $exam_charge= fs_get_value("exam_2");//1:全国   2：地方
       // echo  $exam_charge;exit();
	if(intval(fs_get_value("test_id")) == '' || intval(fs_get_value("test_id")) == 0){
	    $sql = "insert into fooeeexam_exam (`exam_name`,`exam_total_finished`,`exam_time`,`exam_needmoney`,`exam_year`,`exam_basepoint`,
                `exam_total_point`,`exam_class`,`exam_source`,`exam_type_1`,`exam_charge`) value ('".$test_name."',0,".$test_time.",".$test_point.",".$test_year.",
                    ".$test_score.",".$test_total_score.",".$cate.",'".$exam_source."','".$exam_type_1."','".$exam_charge."')";
	    //exit(print_r($sql));
            $db -> query($sql);
        $test_id = mysql_insert_id();
	    if($test_id != ''){
	        //fs_showmsg("添加成功！","admin_test.php");
			echo("<script>if(confirm('添加成功！是否继续添加习题?')){window.location.href = 'admin_test.php?do=addtest';}else{window.location.href = 'admin_test.php';}</script>");
	    }
	}
	else
	{
		$test_id = fs_get_value("test_id");
		$sql = "update fooeeexam_exam set exam_name = '".$test_name."',exam_time=".$test_time.",exam_source='".$exam_source."',exam_needmoney="
                        .$test_point.",exam_year=".$test_year.",exam_basepoint=".$test_score.",exam_total_point=".$test_total_score.",exam_class=".$cate.",exam_type_1='".$exam_type_1."',exam_charge='".$exam_charge."'
                        where exam_id = ".$test_id;
		$db -> query($sql);
		fs_showmsg("修改成功！","admin_test.php");
	}
}
?>
<!--
编缉习题标题结束
-->



<!--
编缉题目分类开始
-->
<?php
function veiwcat(){
    global $db;
	$test_id = intval(fs_get_value("ID"));
	$catid = intval(fs_get_value("catid"));
	if(fs_get_value("title") != '' || fs_get_value("point")){
		$title = fs_get_value("title");
		$point = fs_get_value("part_ex");
		$part_ex = fs_get_value("part_ex");
		$num = fs_get_value("num");
		if($catid == ''){
			//添加分类
		    $sql = "insert into fooeeexam_exam_part (`exam_id`,`exam_part_title`,`exam_part_point`,`exam_part_num`) value (".$test_id.",'".$title."',".$point.",'".$num."')";
		    $db -> query($sql);
		    $id = mysql_insert_id();
		    if($id != ''){
			    fs_showmsg("添加成功！");
		    }
		}
		else
		{
			//修改分类
			$sql = "update fooeeexam_exam_part set exam_part_title = '".$title."',exam_part_point = ".$part_ex.",exam_part_num = ".$num." where exam_part_id = ".$catid;
			$db -> query($sql);
			fs_showmsg("修改成功！");
		}
	}
	if(fs_get_value("delete_id") != ''){
		//删除分类
		$id = fs_get_value("delete_id");
		$sql = "delete from fooeeexam_exam_part where exam_part_id = ".$id;
		$db -> query($sql);
		fs_showmsg("删除成功！");
	}
	$sql = "select * from fooeeexam_exam_part where exam_id = ".$test_id;
	$test_result = $db -> query($sql);
	$recordcount = $db -> num_rows($test_result);
	@$page=$_GET['p'];
	if ($page==null){$page=1;}
	$pagesize=12;								//每页显示数
	$pagecount=ceil($recordcount/$pagesize);	//计算总页
	if ($page>$pagecount){$page=$pagecount;}
	if ($page<1){$page=1;}
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  <tr>
    <th class="CTitle">题目分类管理</th>
  </tr>
  <tr>
    <td class="CPanel">
    <div class="SubMenu">
		<a href="admin_test.php?">习题管理</a> | 
		<a href="admin_test.php?do=Mancat&ID=<?php echo ($test_id);?>">添加分类</a> | 
		<a href="admin_test_record.php?">习题记录</a>|
                <a href="admin_test.php?do=batimport">批量导入</a>
	</div>
	
	<form action="?do=Mancat&ID=<?php echo ($test_id);?>&catid=<?php echo($catid);?>" method="post" name="fooeeform" style="margin:0px">
		<table border="0" cellpadding="0" cellspacing="0"  class="TablePanel" width="100%">
			<tr class="TDHead">
				<td align="left" nowrap style="padding-left:30px;">分类名称：
                                    <input type=text name="title" value="<?php echo(editcat($catid,'exam_part_title'));?>" style="width:160px; color:#FF0000; font-weight:bold;"/>
                                    &nbsp;&nbsp;&nbsp;&nbsp;每小题分数：<input type=text name="part_ex" value="<?php echo(editcat($catid,'exam_part_point'));?>" style="width:400px; color:#FF0000; font-weight:bold;"/>&nbsp;&nbsp;&nbsp;&nbsp;排列序号：
                                    <input type=text name="num" value="<?php echo(editcat($catid,'exam_part_num'));?>" style="width:30px; color:#FF0000; font-weight:bold;"/>
                                   <!-- <input type="hidden" name="point" value="0" />&nbsp;&nbsp;&nbsp;&nbsp;-->
                                    <input type="submit" style="font-size:9pt;width:100px;" value="增加/修改分类" />
				</td>
			</tr>
		</table>
<div id="click_idall">
    <div align="left" style="padding:5px;">
      <table border="0" cellpadding="0" cellspacing="0" class="TablePanel" align="center" width="100%">
        <tr align="center">
		  <td class="TDHead">&nbsp;</td>
          <td class="TDHead" nowrap>题目分类</td>
		  <td class="TDHead">排列序号</td>
          <td class="TDHead" nowrap>习题标题</td>
		  <td class="TDHead" nowrap>现有题目数量</td>
          <td class="TDHead" nowrap>操作分类</td>
          <td class="TDHead" nowrap>编缉试题</td>
        </tr>
<?php
	$sql = "select * from fooeeexam_exam_part where exam_id = ".$test_id." order by exam_part_num limit ".($page-1)*($pagesize).",$pagesize";
	$test_result=$db->query($sql);
	$i = ($page-1)*($pagesize);
	while($test_row = $db->fetch_array($test_result))
	{
		$i = $i + 1;
?>
		<tr align="center">
			<td align="center" width="20"><?php echo($i);?></td>
			<td><div style="width:200px; overflow:hidden; text-align:center; height:24px; line-height:24px;"><a href="?do=Mancat&catid=<?php echo ($test_row["id"]);?>&ID=<?php echo ($test_id);?>" title="<?php echo($test_row["exam_part_title"]);?>"><?php echo($test_row["exam_part_title"]);?></a></div></td>
			<td align="center"><?php echo($test_row['exam_part_num']);?></td>
			<td><?php echo(get_testinfo($test_id,'exam_name'));?></td>
			
			<td><?php echo (get_question_num('exam_part_id',$test_row['exam_part_id']));?></td>
			<td><a href="?do=Mancat&catid=<?php echo ($test_row["exam_part_id"]);?>&ID=<?php echo ($test_id);?>">编缉</a>&nbsp;/&nbsp;<a href="?do=Mancat&delete_id=<?php echo ($test_row["exam_part_id"]);?>">删除</a></td>
			<td><a href="?do=editquestion&test_id=<?php echo ($test_row["exam_id"]);?>&part_id=<?php echo($test_row['exam_part_id']);?>">编缉试题</a></td>
		</tr>
		<tr>
			<td height=1 colspan=9 bgcolor="#E8F1FF" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='E7sxE7sxE7sx'"></td>
		</tr>
<?php
	}
?>
	</table>
	</form>
	
	
	
	</div>
	<center>
<?php
/*分页示例:
pager(传入URL , 数据总数 , 开始条数 , 每页数目)
*/
echo(pager('?p={page}',$recordcount,($page-1)*($pagesize),$pagesize));		
?>
</center>
	</td>
	</tr>
	</table>
<?php
}
function editcat($id,$str){
    global $db;
	if($id != ''){
	    $sql = "select * from fooeeexam_exam_part where exam_part_id = ".$id;
	    $test_result = $db -> query($sql);
	    $test_row = $db -> fetch_array($test_result);
	    return $test_row[$str];
	}
}
?>
<!--
编缉题目分类结束
-->


<?php
/*
编辑习题表单
*/
function editquestion()
{
    global $db;
    $test_id = intval(fs_get_value("test_id"));
	$part_id = intval(fs_get_value("part_id"));
	$keywords = fs_get_value("keywords");
	if($keywords == ''){
        $sql = "select * from fooeeexam_exam_question where exam_id=".$test_id." and exam_part_id = ".$part_id." order by exam_question_id asc"; 
	}
	else{
		$sql = "select * from fooeeexam_exam_question where exam_id=".$test_id." and exam_part_id = ".$part_id." and title like '%".$keywords."%' order by exam_question_id asc"; 
	}
        $test_result = $db -> query($sql);
	$question_num = $db -> num_rows($test_result);
?>
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">习题管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
			<div class="SubMenu">
				<a href="admin_test.php?">习题管理</a> | 
				<a href="admin_test.php?do=edit_question&test_id=<?php echo($test_id);?>&part_id=<?php echo($part_id);?>">添加题目</a> | 
				<a href="admin_test_record.php?">习题记录</a>|
                                <a href="admin_test.php?do=batimport">批量导入</a>
			</div>
				<form action="?do=editquestion&test_id=<?php echo ($test_id);?>&part_id=<?php echo($part_id);?>" method="post" name="fooeeform" style="margin:0px">
		<table border="0" cellpadding="0" cellspacing="0"  class="TablePanel" width="100%">
			<tr class="TDHead">
				<td align="center" nowrap>查询关键字：<input type=text name="keywords" value="" style="width:480px;"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" style="font-size:9pt;width:100px;" value="查询" />
				</td>
			</tr>
		</table>
		</form>
			</td>
		</tr>	
		<tr>
			<td>
			<div class="middle_right_list" style="width:100%;height:580px;padding-left:0px;">
			<div style="width:96%;height:500px;text-align:left;overflow:scroll;overflow-x:auto;border:#7693CF 1px solid;margin-bottom:5px;padding:5px 0px;margin:0px auto;">
			<table border="0" cellpadding="0" cellspacing="0">
			    <tr>
				    <td width="100" height="27"><div align="right">习题标题：</div></td>
					<td><input name="test_name" type="text" size="80" class="text" value="<?php echo (get_testinfo($test_id,'exam_name'));?>" disabled><td>
				</tr>
				<tr>
				    <td width="100" height="27"><div align="right">题目分类：</div></td>
					<td><input name="test_type" type="text" size="100" class="text" value="<?php echo (get_partinfo($part_id,'exam_part_title'));?>" disabled></td>
				</tr>
			</table>
				<div class="middle_right_list_contentt" style="height:445px;">
	<?php
	if($question_num == 0){
	?>
	            <div style="border-bottom:#FF0000 1px dashed;">
				  暂无题目，<a href="?do=edit_question&test_id=<?php echo($test_id);?>&part_id=<?php echo($part_id);?>" class="lj">现在增加题目</a>？
			</div>
	<?php
	}
	else{
	    $a = 0;
        while($test_row = $db -> fetch_array($test_result)){
	?>
	           <div style="border-bottom:#FF0000 1px dashed;">
			    <table border="0" cellpadding="0" cellspacing="0">
				    <tr>
                        <td width="100" height="27"><div align="right" style="line-height:27px;"><b>&nbsp;&nbsp;<?php echo ($a+1);?>.&nbsp;&nbsp;习题题目：</b></div></td>
						<td><span style="line-height:21px; padding-top:3px; color:#FF0000;"><?php echo ($test_row['exam_question_title']); ?></span></td>
					</tr>
					<tr>
					    <td width="100" height="27" valign="top"><div align="right" style="line-height:27px;"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题型选择：</b></div></td>
						<td>
					    <?php
		                    switch($test_row['exam_question_type']){
		                    case 0: echo("单项选择题"); break;
		                    case 1: echo("多项选择题"); break;
		                    case 2: echo("判断题"); break;
		                    case 3: echo("不定项选择题"); break;
		                    }
		                ?></td>
					</tr>
					<tr>
					    <td width="100" height="27" valign="top"><div align="right" style="line-height:27px;"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;正确答案：</b></div></td>
						<td><span><?php echo($test_row['exam_question_answer']);?></span></td>
					</tr>
					<tr>
					    <td width="100" height="27" valign="top"><div align="right" style="line-height:27px;"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;习题答案：</b></div></td>
						<td style="color:#FF0000; line-height:27px;"><?php echo(get_test_answerinfo($test_row['exam_question_id'],'exam_answer_content')); ?></td>
					</tr>
					<tr>
					    <td width="100" height="27" valign="top"><div align="right" style="line-height:27px;"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;答案解析：</b></div></td>
						<td style="line-height:27px;"><?php echo($test_row['exam_question_resolve']); ?></td>
					</tr>
					<tr>
					    <td width="100" height="27" valign="top"></td>
						<td><a class="lj" onclick="return confirm('删除题目会将该题目下的答案和解析一齐删除。你确定删除吗？')" href="?do=delete_question&test_question_id=<?php echo ($test_row['exam_question_id']);?>" class="lj">删除</a></label>&nbsp;&nbsp;&nbsp;&nbsp;<a href="?do=edit_question&test_question_id=<?php echo ($test_row['exam_question_id']);?>&test_id=<?php echo($test_id);?>&part_id=<?php echo($part_id);?>&a=<?php echo($a);?>" class="lj">编缉</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="?do=edit_question&test_id=<?php echo($test_id);?>&part_id=<?php echo($part_id);?>" class="lj">添加题目</a></td>
					</tr>
				</table>
			 </div>
	<?php
		    $a = $a + 1;
        }
	}
	?>
			</div>
			</div>
			<div style="margin:0px auto; line-height:24px; border:#98BBD7 1px solid; width:120px; text-align:center; background-color:#EFF6F8;"><a href="?do=Mancat&ID=<?php echo ($test_id);?>" class="lj">返回题目分类列表</a></div>
			</div>
			</td>
		</tr>
	</table>
<?php
}
//根据试卷ID返回试卷的相关信息
function get_testinfo($test_id,$str)
{
    global $db;
	$sql = "select * from fooeeexam_exam where exam_id=".$test_id;
	$test_result = $db -> query($sql);
	$test_row = $db -> fetch_array($test_result);
	return $test_row[$str];
}
function get_partinfo($part_id,$str)
{
    global $db;
	$sql = "select * from fooeeexam_exam_part where exam_part_id = ".$part_id;
	$test_result = $db -> query($sql);
	$test_row = $db -> fetch_array($test_result);
	return $test_row[$str];
}
function get_test_answerinfo($test_question_id,$str)
{
    global $db;
	$sql = "select * from fooeeexam_exam_anwser where exam_question_id = ".$test_question_id;
        //echo $sql;
	$test_result = $db -> query($sql);
	$test_row = $db -> fetch_array($test_result);
	return $test_row[$str];
}
//取得题目数量
function get_question_num($arg,$test_id)
{
    global $db;
	$sql = "select * from fooeeexam_exam_question where ".$arg." = ".$test_id;
	$test_result = $db -> query($sql);
	return $db -> num_rows($test_result);
}

//删除题目
function delete_question()
{
    global $db;
	$test_question_id = intval(fs_get_value("test_question_id"));
	//先删除题目下的答案
	$sql = "delete from 7sx_test_answer where test_question_id = ".$test_question_id."";
	$db -> query($sql);
	//再删除题目
	$sql = "delete from 7sx_test_question where test_question_id = ".$test_question_id."";
	$db -> query($sql);
	fs_showmsg('删除成功!');
}
/*
编缉习题表单结束
*/
?>
<?php
/*编缉习题题目开始*/
function edit_question()
{
	global $db;
        $test_id = intval(fs_get_value("test_id"));
	$part_id = intval(fs_get_value("part_id"));
	$test_question_id = fs_get_value("test_question_id");
	$a = fs_get_value("a");
	//验证接收到的数字参数是否为空
	if($test_question_id != ''){
		$sql = "select * from fooeeexam_exam_question where exam_question_id = ".$test_question_id;
		$test_result = $db -> query($sql);
		$test_row = $db -> fetch_array($test_result);
	}
?>
<script>
function select_type(){
	var question_type = document.add_questionform.question_type.value;
	if (question_type == 2)
	{
		FCKeditorAPI.GetInstance("test_answer").SetHTML("<p>A、正确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; B、错误</p>");
	}
}
</script>
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">习题管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
			<div class="SubMenu">
				<a href="admin_test.php?">习题管理</a> | 
				<a href="admin_test.php?do=edit_question&test_id=<?php echo($test_id);?>&part_id=<?php echo($part_id);?>">添加题目</a> | 
				<a href="admin_test_record.php?">习题记录</a>|
                                <a href="admin_test.php?do=batimport">批量导入</a>
			</div>
			</td>
		</tr>	
		<tr>
			<td>
			<div class="middle_right_list">
			<form name="add_questionform" method="post" action="?do=do_edit_question&test_id=<?php echo($test_id);?>&part_id=<?php echo($part_id);?>&test_question_id=<?php echo($test_question_id);?>">
			<fieldset class="fieldset_id">
					<label><div align="right">习题标题：</div></label><input name="test_name" type="text" size="80" class="text" value="<?php echo (get_testinfo($test_id,'exam_name'));?>" disabled><br/>
					<label><div align="right">题目分类：</div></label><input name="test_type" type="text" size="100" class="text" value="<?php echo (get_partinfo($part_id,'exam_part_title'));?>" disabled><br/>
				<div class="middle_right_list_contentt" style="height:445px;">
					<label><div align="right"><b>&nbsp;&nbsp;<?php if($test_question_id == ''){echo(get_num($part_id)+1);}else{echo($a+1);}?>.&nbsp;&nbsp;习题题目：</b></div></label><?php
                                                                include("../fckeditor/fckeditor.php");
                                                                $oFCKeditor = new FCKeditor('title');
                                                                $oFCKeditor -> BasePath = "../fckeditor/";
                                                                $oFCKeditor -> Value = $test_row['exam_question_title'];
								$oFCKeditor -> Width = '86%';
								$oFCKeditor -> Height = '120';
                                                                $oFCKeditor -> Create();
	                            ?><br/>
					<label><div align="right"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题型选择：</b></div></label>
					    <select name="question_type" style="margin-top:4px; background-color:#EFF6FB; border:#000000 1px solid; width:120px;" onChange="select_type()">
                            <option value="0" <?php if($test_row['exam_question_type'] == 0){echo("selected");}?>>单项选择题</option>
                            <option value="1" <?php if($test_row['exam_question_type'] == 1){echo("selected");}?>>多项选择题</option>
                            <option value="2" <?php if($test_row['exam_question_type'] == 2){echo("selected");}?>>判断题</option>
                            <option value="3" <?php if($test_row['exam_question_type'] == 3){echo("selected");}?>>不定项选择题</option>
                        </select><br/>
					<label><div align="right"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;正确答案：</b></div></label>
					    <input type="text" name="answer" class="text" value="<?php echo($test_row['exam_question_answer']);?>"/><br />
                    <label><div align="right"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;习题答案：</b></div></label><?php
                                include("../fckeditor/fckeditor.php");
                                $oFCKeditor = new FCKeditor('test_answer');
                                $oFCKeditor -> BasePath = "../fckeditor/";
								if($test_row['exam_question_id'] == ''){
									$oFCKeditor -> Value = '';
								}
								else
	                            {
                                    $oFCKeditor -> Value = get_test_answerinfo($test_row['exam_question_id'],'exam_answer_content');
								}
								$oFCKeditor -> Width = '86%';
								$oFCKeditor -> Height = '120';
                                $oFCKeditor -> Create();
	                            ?><br/>
                    <label><div align="right"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;答案解析：</b></div></label><?php
                                include("../fckeditor/fckeditor.php");
                                $oFCKeditor = new FCKeditor('exam_question_resolve');
                                $oFCKeditor -> BasePath = "../fckeditor/";
                                $oFCKeditor -> Value = $test_row['exam_question_resolve'];
								$oFCKeditor -> Width = '86%';
								$oFCKeditor -> Height = '120';
                                $oFCKeditor -> Create();
	                            ?><br />
					<div class="submit" align="center"><input type="submit" value="确认提交" onclick="return addcheck();"/> <a class="lj" href="?do=editquestion&test_id=<?php echo($test_id);?>&part_id=<?php echo($part_id);?>">返回题目列表</a></div>
			</div>
			</fieldset>
			</form>
			</div>
			</td>
		</tr>
	</table>
<?php
}
function import(){
    global $db;
    $exam_rows =$db->query("select * from fooeeexam_exam");
    
    ?>
<style>
    .importf form{
        width:330px;
        padding:8px;
    }
    input{
        
    }
    .importf .note{
        font-size:12px;
        color:red;
    }
    .importf .title{
        font-size:12px;
    }
    .importf p{
       /* text-align: center;*/
    }
</style>
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
function getPart(examid){
    var url="getparts.php?examid="+examid;
    $.ajax({
         url: url,
         cache: false,
         dataType: "html",
         success:function(html){
			　 $("#parts").html(html);
			  }
    });
}
</script>
<div class="importf" style="width:330px;margin: 100px auto;">
    <form action="?do=doimport" method="post" enctype="multipart/form-data">
        <span class="title">试卷名称:</span><select name="examname" onchange="getPart(this.value);">
            <option value="">请选择考试名称...</option>
           <?php while($rows = $db->fetch_array($exam_rows)){?>    
            <option value="<?php echo $rows['exam_id']?>">
                <?php echo $rows['exam_name']?>
            </option>
        <?php }?>
        </select><br/><br/><div id="exparts"></div>
        <span class="title">部分名称:</span>
        <select id="parts" name="parts">
            <option value="">请选择部分名称...</option>
        </select><br/><br/>
        <span class="title">试题类型:</span>
        <select id="type" name="type">
        <option value="">请选择试题类型...</option>
        <option value="0">单选题</option>
        <option value="1">多选题</option>
        <option value="2">判断题</option>
          </select><br/><br/>
        <span class="title">试题导入:</span><input type="file" name="file"/><span class="note">仅支持txt文档</span><br/>
        <div style="width:200px;text-align: center;margin-top: 10px"><input type="submit" value="导入" name="sub" /></div>
        <div style="text-align:left;">1:试题必须为选择题或判断题。<br/>2：必须把试题拷贝到文本文档中进行处理。处理的样式参加下面：<br/>
                <p style="text-align:left;width:500px;">1、建筑安装工程中生产工人的产、婚、丧假期的工资属于()<span style="color:red">-split-question-</span></p>
<p>A.工资性补贴</p>
<p>B.职工福利费</p>
<p>C.生产工人辅助工资</p>
<p>D.生产工人劳动保护费 <span style="color:red">-split-question-</span></p>
<p>C</p>
<p><span style="color:red">-split-test-</span></p>
<p>2、按建标【2003】206号文的规定，在建安工程造价中，安全施工费=( 　)×安全施工费费率 <span style="color:red">-split-question-</span></p>
<p>A.人工费</p>
<p>B.直接工程费</p>
<p>C.材料费</p>
<p>D.直接费 <span style="color:red">-split-question-</span></p>
<p>B</p>
<p><span style="color:red">-split-test-</span></p>
<p>3：红色部分为分隔符 每个题中的分隔符与题与题间的分隔符不同</p>
<p>4:如果有解析，另起一行</p>
<p>5:存储在文本框里面的答案选项ABCD必须是常规写法，不能使用其他非常规写法Ａ、Ｃ、等均属于非常规写法</p>
            </div>
    </form>  
</div>
<?php
}
function doimport(){
    /* 设定上传目录 */
    $dest_dir='uploadfiles';
 
    /* 检测上传目录是否存在 */
    if( !is_dir($dest_dir) || !is_writeable($dest_dir) )
    {
        die("上传目录 ".$dest_dir." 不存在或无法写入");
    }
 
    /* 设置允许上传文件的类型 */
    $type=array("txt");
 
    /* 获取上传文件信息 */
    $upfile=$_FILES['file'];
 //print_r($upfile);
    /* 获取文件后缀名函数 */
    function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }
    /* 判断上传文件类型 */
    if( !in_array( strtolower( fileext($upfile['name'] ) ),$type) )
     {
        $text=implode(",",$type);
        echo "对不起，您只能上传以下类型文件: ",$text,"<br>";
     }
     else
     {
        /* 设置文件名为"日期_文件名" */
        $dest=$dest_dir.'/'.time().$upfile['name'];
 
        /* 移动上传文件到指定文件夹 */
        $state=move_uploaded_file($upfile['tmp_name'],$dest);
 
        if ($state)
        {
            print("文件上传成功!<br>");
            print("文件名：".$dest."<br>");
            print("上传的文件大小：".( round($upfile['size'] / 1024,2) )." KB<br>");
        }
        else
        {
            /* 处理错误信息 */
            switch($upfile['error'])
            {
                case 1 : die("上传文件大小超出 php.ini:upload_max_filesize 限制<br>");
                case 2 : die("上传文件大小超出 MAX_FILE_SIZE 限制<br>");
                case 3 : die("文件仅被部分上传<br>");
                case 4 : die("没有文件被上传<br>");
                case 5 : die("找不到临时文件夹<br>");
                case 6 : die("文件写入失败<br>");
            }
        }
     }
    $handle=fopen($dest,"r");
    $content=fread($handle,filesize($dest));//读文件
    $content=iconv("gbk","utf-8//IGNORE",$content);
    fclose($handle);
    $rows =explode('-split-test-',$content);
    foreach($rows as $key=>$v){   
        $v =explode('-split-question-',trim($v));//将每个问题 拆分为  问题标题-答案选项-答案 三个部分
        $v[1]=trim($v[1]);
        
        preg_match_all("/[ABCDE]+/i",$v[0],$answer_in_question);//从每个问题中的问题标题部分提取答案
        
        if($answer_in_question[0][0]) 
        {   
            $v[2]=strtoupper($answer_in_question[0][0]);
            $preg_str=preg_replace("/[ABCDE]+/i",'',$v[0]);//删除 问题标题中的答案
            $v[0] = $preg_str;//把格式化的问题标题重新赋值到问题标量上
        }
        
        if(substr_count($v[1], 'A')>1||substr_count($v[1], 'B')>1||substr_count($v[1], 'C')>1||substr_count($v[1], 'D')>1||substr_count($v[1], 'E')>1)
        {
        }
        else
        {
            $v_arr=preg_split('/[ABCDE]+/',$v[1],-1,PREG_SPLIT_NO_EMPTY);
            $v_str='';
            
            foreach($v_arr as $key=>$v_an){
                switch ($key){
                        case 0:$answer_key='A';break;
                        case 1:$answer_key='B';break;
                        case 2:$answer_key='C';break;
                        case 3:$answer_key='D';break;
                        case 4:$answer_key='E';break;
                        default:$answer_key='';
                }
                $v_str .=$answer_key.$v_an.'<br/>';
                $v[1]=$v_str;
            }  
        } 
            global $db;
            $exam_part=fs_get_value('examname');
            $exam_part_id =fs_get_value('parts');
            $exam_question_type =fs_get_value('type'); 
            $inser_id=$db->query("insert into fooeeexam_exam_question (`exam_id`,`exam_part_id`,`exam_question_type`,`exam_question_title`,`exam_question_answer`,`exam_question_resolve`) values(".$exam_part.",".$exam_part_id.",".$exam_question_type.",'".$v[0]."','".$v[2]."','".$v['3']."')");
            $question=mysql_insert_id();
            $result=$db->query("insert into fooeeexam_exam_anwser (`exam_question_id`,`exam_answer_content`) values(".$question.",'".$v[1]."')");
         }
        if($result)
        {
            fs_showmsg('导入成功');
        }
    
}
function get_num($part_id){
	global $db;
	$sql = "select * from fooeeexam_exam_question where exam_part_id =".$part_id;
	$result = $db -> query($sql);
	$num = $db -> num_rows($result);
	return $num;
}
/*修改*/
function do_edit_question()
{
    global $db;
	$test_id = intval(fs_get_value("test_id"));
	$part_id = intval(fs_get_value("part_id"));
	$test_question_id = intval(fs_get_value("test_question_id"));
	$title = fs_get_value("title");
	$question_type = fs_get_value("question_type");
	$answer = fs_get_value("answer");
	$test_answer = fs_get_value("test_answer");
	$test_resolve = fs_get_value("test_resolve");
	//如果解析为空，则赋值
	if($test_resolve == ''){
		$test_resolve = "暂无解析！";
	}
	if($title == '' || $answer == '' || $question_type == '' || $test_answer == ''){
		fs_showmsg("不能为空，请重新填写！");
	}
	if($test_question_id == 0){
		//对数据进行插入
        $sql = "insert into fooeeexam_exam_question (exam_id,exam_question_title,exam_part_id,exam_question_resolve,exam_question_answer,exam_question_type) value (".$test_id.",'".$title."',".$part_id.",'".$test_resolve."','".trim($answer)."',".$question_type.")";
		$db -> query($sql);
		$test_question_id = mysql_insert_id();
		$sql = "insert into fooeeexam_exam_anwser (exam_question_id,exam_answer_content) value (".$test_question_id.",'".$test_answer."')";
		$db -> query($sql);
		//fs_showmsg("添加成功！");
		echo("<script>if(confirm('添加成功！是否继续添加题目?')){window.location.href = 'admin_test.php?do=edit_question&test_id=".$test_id."&part_id=".$part_id."';}else{window.location.href = 'admin_test.php?do=editquestion&test_id=".$test_id."&part_id=".$part_id."';}</script>");
	}
	else
	{
		//对数据进行修改
		$sql = "update fooeeexam_exam_question set exam_question_title = '".$title."',exam_question_resolve = '".$test_resolve."',exam_question_answer = '".trim($answer)."',exam_question_type = ".$question_type." where exam_question_id = ".$test_question_id;
		$db -> query($sql);
		$sql = "update fooeeexam_exam_anwser set exam_answer_content = '".$test_answer."' where exam_question_id = ".$test_question_id;
		$db -> query($sql);
		fs_showmsg("编缉成功！");
	}
}
/*编缉习题题目结束*/
?>

<?php
//删除试卷
function delete_test()
{
    global $db;
	$i=0;
	$test_id = $_POST['id'];
	while($test_id){
	    if($test_id[$i]==null) break;
		$sql = "select * from fooeeexam_exam_question where exam_id = ".$test_id[$i]."";
		$test_result = $db -> query($sql);
		while ($test_row=$db -> fetch_array($test_result))
		{
		    //删除习题答案
		    $sql = "delete from fooeeexam_exam_anwser where exam_question_id = ".$test_row['test_question_id']."";
		    $db -> query($sql);
		}
		//删除习题题目
		$sql = "delete from fooeeexam_exam_question where exam_id = ".$test_id[$i]."";
		$db -> query($sql);
		//删除题目类别
		$sql = "delete from fooeeexam_exam_part where exam_id = ".$test_id[$i];
		$db -> query($sql);
		//删除习题标题
		$sql = "delete from fooeeexam_exam where exam_id =".$test_id[$i];
	    $db -> query($sql);
	    $i++;
	}
	fs_showmsg('删除成功!');
}
function do_delete_question()
{
    global $db;
    $question_id = fs_get_value('test_question_id');
    $del_question = $db->query("delete from ".TABLE_EXAM_QUESTION." where exam_question_id = ".$question_id);
    $del_answer = $db->query("delete from ".TABLE_EXAM_ANWSER." where exam_question_id= ".$question_id);
    if($del_question && $del_answer)
    {
        fs_showmsg('问题以及该问题的相关选项删除成功！');
    }
}
?>